package com.gr.blog.upload.action;


import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.gr.blog.jira.service.JiraService;
import com.gr.blog.utils.CommonUtils;
import com.gr.blog.utils.ExcelUtils;

/**
 * 多文件或者单文件上传的action
 * @author:巩斌鹏
 * 2018年5月22日 上午9:39:12
 */
@Controller
@RequestMapping("fileLoadAction")
public class FileLoadAction {

	private static final Logger logger = Logger.getLogger(FileLoadAction.class);
	@Autowired
	@Qualifier("jiraService")
	private JiraService jiraService;
	
	@RequestMapping("fileUploadDate")
	public void batchFielUpload(HttpServletRequest request,HttpServletResponse response,
								@RequestParam("file") MultipartFile file) throws Exception{
		
		ExcelUtils exu = new ExcelUtils();
		
		if (null != file) {
			String filename = file.getOriginalFilename();
			long size =file.getSize();
			if (CommonUtils.isNotEmpty(filename) && size > 0) {
				exu.checkExcelVaild(filename);
				//把spring文件上传的MultipartFile转换成CommonsMultipartFile类型
		        InputStream iStream = file.getInputStream();
		        Workbook wb = exu.getWorkbok(iStream, filename);
		        Sheet sheet = wb.getSheetAt(0);
		        int totalRows = sheet.getPhysicalNumberOfRows();
		        int totalCells = 0;
		        if (totalRows >= 1 && sheet.getRow(0) != null) {
		        	totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
		        }
		        
		        for(int r=1;r<totalRows;r++){
		        	Row row = sheet.getRow(r);
		        	for (int c = 0; c < totalCells; c++) {
		        		response.getWriter().print(c/size);
		        		Map<String,Object> jiraMap = new HashMap<>();
		        		Cell username = row.getCell(1);
		        		Cell jiarNm = row.getCell(2);
		        		Cell isOnline = row.getCell(3);
		        		Cell isOwerSqlBat = row.getCell(4);
		        		Cell onlineTime = row.getCell(5);
		        		Cell codeDivision = row.getCell(6);
		        		Cell implPrinciple = row.getCell(7);
		        		Cell codeLine = row.getCell(8);
		        		Cell codeNote = row.getCell(9);
		        		//String key = sheet.getRow(0).getCell(c).getStringCellValue();得到表头
		        		jiraMap.put("jid", CommonUtils.getUUID32Bit());
		        		jiraMap.put("username", exu.vildCellType(username));
		        		jiraMap.put("jiarNm", exu.vildCellType(jiarNm));
		        		jiraMap.put("isOnline", exu.vildCellType(isOnline).substring(0, 1));
		        		if (0 == onlineTime.getCellType()){
		        			if (DateUtil.isCellDateFormatted(onlineTime)){
		        				Date d = onlineTime.getDateCellValue();
		        				DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
		        				jiraMap.put("onlineTime", formater.format(d));
		        			}
		        		}
		        		jiraMap.put("isOwerSqlBat", exu.vildCellType(isOwerSqlBat).substring(0, 1));
		        		jiraMap.put("codeDivision", exu.vildCellType(codeDivision));
		        		jiraMap.put("implPrinciple", exu.vildCellType(implPrinciple));
		        		jiraMap.put("codeLine", exu.vildCellType(codeLine));
		        		jiraMap.put("codeNote", exu.vildCellType(codeNote));
		        		logger.error("[excel导入后最终插入db的数据]："+jiraMap);
		        		if (null != jiraMap && jiraMap.size() >0){
		        			jiraService.suibmitJirainfo(jiraMap);
		        		}
		        	}
		        }
			} else {
				return;
			}
		} else {
			return;
		}
	}	
}
